! read and write .mat file in matlab
!#include "fintrf.h"
module mtIO
    
   implicit none
   private
   
   public :: read_main
!   public :: write_main

contains

!==================================================
! read in seismograms and parameters
!

subroutine read_main(ret)

use problem_class

type(problem_type),intent(inout) :: ret

integer :: i

write (6,*) 'start reading input parameters: ...'
open(unit=15, FILE='par.in')

read(15,*)ret%sr
read(15,*)ret%n
read(15,*)ret%m
read(15,*)ret%ps
read(15,*)ret%qs

read(15,*)ret%ori
read(15,*)ret%begin
read(15,*)ret%over
read(15,*)ret%winLength
read(15,*)ret%Nw
read(15,*)ret%P
read(15,*)ret%npts
read(15,*)ret%Mm
read(15,*)ret%fl
read(15,*)ret%fh

read(15,*)ret%step
read(15,*)ret%lat0
read(15,*)ret%lon0
write(6,*) 'number of element: ...'
write(6,*)ret%m

read(15,*)ret%uxRange
read(15,*)ret%uyRange

allocate (ret%lat(ret%n))
read(15,*)ret%lat

allocate (ret%lon(ret%n))
read(15,*)ret%lon

allocate (ret%rdis(ret%n))
read(15,*)ret%rdis

allocate (ret%az(ret%n))
read(15,*)ret%az
write(6,*) 'azimuth(1): ...'
write(6,*)ret%az(1)


allocate (ret%nm(ret%n,5))
allocate (ret%r(ret%n,2))

do i=1,ret%n
read(15,*)ret%nm(i,:)
end do

write(6,*) 'station name(10): ...'
write(6,*)ret%nm(10,:)

do i=1,ret%n
read(15,*)ret%r(i,:)
end do

write(6,*) 'r(1): ...'
write(6,*)ret%r(1,1)

read(15,*)ret%rtn
allocate (ret%rr(ret%rtn))
read(15,*)ret%rr
allocate (ret%tt(ret%rtn))
read(15,*)ret%tt

allocate(ret%E(ret%P,ret%npts))
do i=1,ret%P
read(15,*)ret%E(i,:)
enddo
!===========read data
allocate (ret%x(ret%n,ret%m))
allocate (ret%xori(ret%n,ret%m))

write (6,*) 'start reading input data: ...'
open(unit=16, FILE='data.in')

do i=1,ret%m
read(16,*)ret%x(:,i)
end do

write(6,*) 'x(1): ...'
write(6,*)ret%x(1,1)

do i=1,ret%m
read(16,*)ret%xori(:,i)
end do

end subroutine read_main

end module mtIO
     
